package com.example.myapplication2;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.view.View;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.myapplication2.Service.LocalService;

public class ServiceActivity extends AppCompatActivity implements View.OnClickListener {

    private Intent serviceIntent;
    private ServiceConnection conn;

    private LocalService localService;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service);
        serviceIntent = new Intent(this, LocalService.class);

        findViewById(R.id.StartService).setOnClickListener(this);
        findViewById(R.id.StopService).setOnClickListener(this);
        findViewById(R.id.StartService2).setOnClickListener(this);
        findViewById(R.id.StopService2).setOnClickListener(this);
        findViewById(R.id.ReadServiceDate).setOnClickListener(this);
        findViewById(R.id.Exit).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //开始服务
            case R.id.StartService:
                startService(serviceIntent);
                break;
            case R.id.StopService:
                stopService(serviceIntent);
                break;
                //绑定并开始服务
            case R.id.StartService2:
                if (conn==null){
                    conn=new ServiceConnection() {

                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            LocalService.LocalBinder binder = (LocalService.LocalBinder) service;
                            localService = binder.getService();
                            System.out.println("开始连接");

                        }

                        /**
                         * onServiceDisconnected() 在连接正常关闭的情况下是不会被调用的.
                         * 该方法只在Service 被破坏了或者被杀死的时候调用.
                         * 例如, 系统资源不足, 要关闭一些Services, 刚好连接绑定的 Service 是被关闭者之一,
                         * 这个时候onServiceDisconnected() 就会被调用.
                         * @param name The concrete component name of the service whose
                         * connection has been lost.
                         */
                        @Override
                        public void onServiceDisconnected(ComponentName name) {
                            System.out.println("断开连接");
                            localService=null;
                        }
                    };
                }
                bindService(serviceIntent,conn, Context.BIND_AUTO_CREATE);
                System.out.println("服务已经绑定");
                break;
            case R.id.StopService2:
                if (conn!=null){
                    unbindService(conn);
                    conn=null;
                    System.out.println("服务已经解绑");
                }else {
                    System.out.println("服务不存在");
                }
                break;
            case R.id.ReadServiceDate:
                if (localService!=null){
                    System.out.println("数据:"+localService.getCount());
                }else {
                    System.out.println("localService:为空");
                }
                break;
            case R.id.Exit:
                System.out.println("退出");
                System.exit(0);
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (conn!=null){
            unbindService(conn);
            conn=null;
        }
        System.out.println("舞台销毁");
    }
}
